package com.flowplan.auth.repository;

import com.flowplan.entity.SysUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

/**
 * 用户数据访问层
 */
@Repository
public interface SysUserRepository extends JpaRepository<SysUser, Long> {
    
    /**
     * 根据用户名查找用户
     */
    Optional<SysUser> findByUsername(String username);
    
    /**
     * 根据用户名查找激活用户
     */
    Optional<SysUser> findByUsernameAndIsActiveTrue(String username);
    
    /**
     * 根据邮箱查找激活用户
     */
    Optional<SysUser> findByEmailAndIsActiveTrue(String email);
    
    /**
     * 检查用户名是否存在
     */
    boolean existsByUsername(String username);
    
    /**
     * 检查邮箱是否存在
     */
    boolean existsByEmail(String email);
    
    /**
     * 根据组织ID查找用户列表
     */
    @Query("SELECT u FROM SysUser u WHERE u.orgId = :orgId AND u.isActive = true")
    java.util.List<SysUser> findByOrgIdAndIsActiveTrue(@Param("orgId") Long orgId);
}